
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class ViewAlbum extends List implements CommandListener, Runnable {

    private UpAnh upAnh;
    private Command cmdBack;
    private Command cmdDelete;
    private Alert alert;
    private Command cmdYes;
    private Command cmdNo;

    public ViewAlbum(UpAnh upAnh) {
        super("Album", IMPLICIT);
        this.upAnh = upAnh;
        upAnh.client.ViewAlbum();
        cmdBack = new Command("Back", Command.BACK, 2);
        cmdDelete = new Command("Xoa", Command.SCREEN, 2);
        addCommand(cmdBack);
        addCommand(cmdDelete);
        setCommandListener(this);
        new Thread(this).start();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdBack) {
            upAnh.DisplayViewImage();
        } else if (c == SELECT_COMMAND) {
            upAnh.DisplayViewImageName(getString(getSelectedIndex()), getSelectedIndex());
        } else if (c == cmdDelete) {
            alert = new Alert("UpAnh");
            alert.setType(AlertType.INFO);
            alert.setString("Ban co muon xoa album nay khong?");
            cmdYes = new Command("Co", Command.OK, 2);
            cmdNo = new Command("Khong", Command.BACK, 2);
            alert.addCommand(cmdYes);
            alert.addCommand(cmdNo);
            alert.setCommandListener(this);
            upAnh.display.setCurrent(alert);
        } else if (c == cmdYes) {
            upAnh.display.setCurrent(this);
            upAnh.client.DeleteAlbum(getSelectedIndex());
            try {
                Thread.sleep(200);
            } catch (Exception e) {
            }
            upAnh.client.ViewAlbum();
            new Thread(this).start();
        } else {
            upAnh.display.setCurrent(this);
        }
    }

    public void run() {
        while (true) {
            if (upAnh.client.listAlbum.size() > 0) {
                this.deleteAll();
                for (int i = 0; i < upAnh.client.listAlbum.size() - 1; i++) {
                    append(upAnh.client.listAlbum.elementAt(i).toString(), null);
                }
                upAnh.client.listAlbum = null;
                return;
            }
            System.gc();
        }
    }
}
